/**
 * @author Juko
 * @version 0.1
 */

package ganga.custodian.sandbox;

import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.androidplot.ui.layout.DynamicTableModel;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.LineAndPointRenderer;
import com.androidplot.xy.XYPlot;

import ganga.custodian.R;
import ganga.custodian.plot.BMPSeries;
import ganga.custodian.plot.KIGSeries;

/**
 * Визуализатор монитора
 * @author Juko
 * @version 0.2
 * @updated 14-июл-2011 17:49:48
 */
public class MonitorScreen extends Activity{
 
   
	private XYPlot kigPlot;
    private XYPlot bpmPlot;
    private TextView bpmText;
    private TextView TimeText;
    private TextView realKIG;
    private BMPSeries dinamicBPM;
    private KIGSeries dinamicKIG;
    private LineAndPointFormatter colorKIGSeries;
    private LineAndPointFormatter colorBPMSeries;
    final Vector<Double> dinamicVector = new Vector<Double> ();
    Timer timer;
    double delayBit = 0;
    double nonametime = 0;
    boolean firstBeat = true;
    
    public void bitTimer(){
    	return;
       /* timer = new Timer();
        timer.schedule(new RemindTask(), 0, 1);*/
        }

    class RemindTask extends TimerTask
        {
        public void run(){
            delayBit++;
            nonametime++;
            }
        }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.realtimemonitor);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        bpmText = (TextView)findViewById(R.id.bpmText);
        TimeText = (TextView)findViewById(R.id.timeText);
        kigPlot = (XYPlot)findViewById(R.id.rigPlot);
        bpmPlot = (XYPlot)findViewById(R.id.bpmPlot);
        realKIG = (TextView)findViewById(R.id.realKIG);
        buildXYPlot(kigPlot);
        buildXYPlot(bpmPlot);
        kigPlot.setRangeLabel(getString(R.string.RRInterval));
        bpmPlot.setRangeLabel(getString(R.string.BeatsPerMinute));
        
        dinamicBPM = new BMPSeries(dinamicVector);
        dinamicKIG = new KIGSeries(dinamicVector);
        dinamicKIG.setLabel(getString(R.string.KIG));
        dinamicBPM.setLabel(getString(R.string.BMP));
        bitTimer();
        firstBeat = true;

        registerReceiver(new BroadcastReceiver()
            {
            @Override
            public void onReceive(Context arg0, Intent receivedIntent){
            	Integer newInterval = receivedIntent.getIntExtra("RRInterval", 900);
            	double interval = newInterval.doubleValue();
            	
            	setNewInterval(interval);
            	
            	           	
            }
            }, new IntentFilter("Redraw"));
        }
 

    private void buildXYPlot(XYPlot aSimpleXYPlot){

        aSimpleXYPlot.getGraphWidget().setTicksPerRangeLabel(2);
        aSimpleXYPlot.getGraphWidget().setTicksPerDomainLabel(5);
        aSimpleXYPlot.getGraphWidget().setRangeValueFormat(new DecimalFormat("#####"));
        aSimpleXYPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("#####"));
        aSimpleXYPlot.getGraphWidget().setRangeLabelWidth(25);
        aSimpleXYPlot.setDomainLabel(" ");
        aSimpleXYPlot.disableAllMarkup();
        colorBPMSeries = new LineAndPointFormatter(Color.rgb(0, 155, 255), Color.rgb(80, 0, 255), null);
        colorKIGSeries = new LineAndPointFormatter(Color.rgb(255, 200, 0), Color.rgb(200, 255, 0), null);
        aSimpleXYPlot.getLegendWidget().setTableModel(new DynamicTableModel(0, 0));
        }

    /**
	 * Функция перерисовки графика
	 * 
	 * @param interval    Новый РР интервал в милисекундах
	 */
    public void setNewInterval(double interval){
    	
        if (firstBeat == false){
            if (dinamicVector.size() > 15)
                dinamicVector.remove(0);
            dinamicVector.add(interval);
            kigPlot.addSeries(dinamicKIG, LineAndPointRenderer.class, colorKIGSeries);
            kigPlot.redraw();
            bpmPlot.addSeries(dinamicBPM, LineAndPointRenderer.class, colorBPMSeries);
            bpmPlot.redraw();
            bpmText.setText(Double.toString(Math.round(60 / (interval) * 1000)));
            double msround = Math.round(nonametime);
            TimeText.setText(Double.toString(msround / 1000) + "  Second");
            realKIG.setText(Double.toString(interval));
            }
        else{
            firstBeat = false;
        }
    }

 

}